from drf_openapi.views import SchemaView,OpenApiSchemaGenerator
from rest_framework import response,permissions
from drf_openapi import codec



def _get_field_description(field):
    if getattr(field, 'schema', None) is not None:
       title,description=field.schema.title,field.schema.description
    else:
        title, description = field.title, field.description
    return f'{title} {description}'
    # return field.schema.description

codec._get_field_description=_get_field_description




class MyOpenApiSchemaGenerator(OpenApiSchemaGenerator):
    def get_serializer_fields(self, path, method, view, version=None, method_func=None):
        if method=='GET':
            return []
        return super().get_serializer_fields(path, method, view, version, method_func)


class MySchemaView(SchemaView):
    permission_classes = (permissions.AllowAny,)

    def get(self, request, version):
        generator = MyOpenApiSchemaGenerator(
            version=version,
            url=self.url,
            title=self.title
        )
        return response.Response(generator.get_schema(request))
